﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

using PetriNetSharp.Engine;

namespace PetriNetSharp.View
{
	/// <summary>
	/// Interaction logic for TransitionView.xaml
	/// </summary>
	public partial class TransitionView : UserControl
	{

		/// <summary>
		/// Identifies the Transition Name  property
		/// </summary>
		public static readonly DependencyProperty TransitionNameProperty =
			DependencyProperty.Register("TransitionName", 
			typeof(string), typeof(TransitionView), new PropertyMetadata(""));

		/// <summary>
		/// Identifies the Transiton Name IsEnabled property
		/// </summary>
		public static readonly DependencyProperty TransitionNameIsEnabledProperty =
			DependencyProperty.Register("TransitionNameIsEnabled", typeof(bool), typeof(TransitionView),
			new PropertyMetadata(true));

		/// <summary>
		/// Identifies the Transition Name Foreground Property
		/// </summary>
		public static readonly DependencyProperty TransitionNameForegroundProperty =
			DependencyProperty.Register("TransitionNameForeground", typeof(Brush), typeof(TransitionView),
			new PropertyMetadata(new SolidColorBrush(Colors.White)));


		/// <summary>
		/// Create a new Transition View
		/// </summary>
		public TransitionView()
		{
			InitializeComponent();

			//Bind the name text box and the transition name dependency property
			Binding bind = new Binding();
			bind.Source = this;
			bind.Path = new PropertyPath(TransitionNameProperty);
			bind.Mode = BindingMode.TwoWay;
			bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
			nameTextBox.SetBinding(TextBox.TextProperty, bind);

			//Bind the name text box IsEnabled to TransitionNameIsEnabled Property
			bind = new Binding();
			bind.Source = this;
			bind.Path = new PropertyPath(TransitionNameIsEnabledProperty);
			bind.Mode = BindingMode.TwoWay;
			nameTextBox.SetBinding(TextBox.IsEnabledProperty, bind);

			//Bind the name text boxes foreground and background to TransitionNameForground
			bind = new Binding();
			bind.Source = this;
			bind.Path = new PropertyPath(TransitionNameForegroundProperty);
			bind.Mode = BindingMode.TwoWay;
			nameTextBox.SetBinding(TextBox.ForegroundProperty, bind);
		}

		/// <summary>
		/// Get or set the transition name
		/// </summary>
		public string TransitionName
		{
			get { return (string)GetValue(TransitionNameProperty); }
			set { SetValue(TransitionNameProperty, value); }
		}

		/// <summary>
		/// Get or set whether the Transition name can be edited by the user
		/// </summary>
		public bool TransitionNameIsEnabled
		{
			get { return (bool)GetValue(TransitionNameIsEnabledProperty);}
			set { SetValue(TransitionNameIsEnabledProperty, value); }
		}

		/// <summary>
		/// Get or set the brush used for the transtion name foreground
		/// </summary>
		public Brush TransitionNameForeground
		{
			get { return (Brush)GetValue(TransitionNameForegroundProperty); }
			set { SetValue(TransitionNameForegroundProperty, value); }
		}


		#region focus methods

		/// <summary>
		/// On a mouse down on the rectangle focus is set
		/// </summary>
		/// <param name="sender">Not used at this time</param>
		/// <param name="e">Not used at this time</param>
		private void rectangle_MouseDown(object sender, MouseButtonEventArgs e)
		{
			FocusManager.SetFocusedElement(this.Parent, this);
		}

		/// <summary>
		/// When the control gets focus performs the visual changes
		/// </summary>
		/// <param name="sender">Not used at this time</param>
		/// <param name="e">Not used at this time</param>
		private void UserControl_GotFocus(object sender, RoutedEventArgs e)
		{
			rectangle.SetResourceReference(Rectangle.StrokeThicknessProperty, 
				"SelectedBorderThickness");
		}

		/// <summary>
		/// When the control loses focus performs the visual changes
		/// </summary>
		/// <param name="sender">Not used at this time</param>
		/// <param name="e">Not used at this time</param>
		private void UserControl_LostFocus(object sender, RoutedEventArgs e)
		{
			rectangle.SetResourceReference(Rectangle.StrokeThicknessProperty, 
				"DeselectedBorderThickness");
		}

		/// <summary>
		/// When the text box gets keyboard focus sets logical focus as well
		/// </summary>
		/// <param name="sender">Not used at this time</param>
		/// <param name="e">Not used at this time</param>
		private void nameTextBox_GotKeyboardFocus(object sender, 
			KeyboardFocusChangedEventArgs e)
		{
			FocusManager.SetFocusedElement(Parent, this);
		}

		#endregion

		#region static methods

		/// <summary>
		/// Get the Height of a TransitionView
		/// </summary>
		/// <remarks>
		/// Right now this is kind of a hack, I should see if there is a 
		/// better way to look this up
		/// </remarks>
		public static double TransitionHeight
		{
			get
			{
				FrameworkElement e = new FrameworkElement();
				return (double)e.FindResource("TransitionHeight");
			}
		}

		/// <summary>
		/// Get the Width of a TransitionView
		/// </summary>
		/// <remarks>
		/// Right now this is kind of a hack, I should see if there is a 
		/// better way to look this up
		/// </remarks>
		public static double TransitionWidth
		{
			get
			{
				FrameworkElement e = new FrameworkElement();
				return (double)e.FindResource("TransitionWidth");
			}
		}

		#endregion
	}
}
